<template>
  <div>
    <!-- 编辑用户弹窗 -->
    <el-dialog title="编辑留言" :visible.sync="dialogVisibleTwo" width="60%">
      <el-form
        :model="messagesEdit"
        status-icon
        ref="messagesEdit"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 留言 -->

        <el-form-item label="留言用户" prop="userName">
          <el-input
            type="text"
            v-model="messagesEdit.userName"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="留言信息" prop="message">
          <el-input
            type="text"
            v-model="messagesEdit.message"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="留言时间" prop="messageTime">
          <el-input
            type="text"
            v-model="messagesEdit.messageTime"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        

        <el-form-item>
          <el-button type="primary" @click="submitUserEditForm('messagesEdit')"
            >确认编辑</el-button
          >
          <el-button @click="resetForm('messagesEdit')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>


    <div>
      <el-table
        :data="
          messages.filter(
            (data) =>
              !search ||
              data.userName.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 100%"
      >
        <el-table-column label="留言 ID" prop="_id"> </el-table-column>
        <el-table-column label="留言用户" prop="userName"> </el-table-column>
        <el-table-column label="留言信息" prop="message"> </el-table-column>
        <el-table-column label="留言时间" prop="messageTime"> </el-table-column>
        <el-table-column align="right">
          <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            placeholder="输入关键字搜索"/>
        </template>
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      messages:[],
      search: "",
      dialogVisibleTwo:false,
      messagesEdit:{
        _id:'',
        userName:'',
        message:'',
        messageTime:''
      }
    };
  },
  methods: {
    handleEdit(value) {
      this.dialogVisibleTwo = true
      this.messagesEdit._id = value._id
      this.messagesEdit.userName = value.userName
      this.messagesEdit.message = value.message
      this.messagesEdit.messageTime = value.messageTime
    },
    handleDelete(value) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.messagesEdit._id = value._id
          this.deleteUserInfo()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 删除用户信息
    deleteUserInfo(){
      axios.post("http://127.0.0.1:3000/deleteMessageInfo", {
              _id: this.messagesEdit._id,
            })
            .then(
              () => {
                this.deleteUser_success();
                this.sendUserEditInfo()
              },
              () => {
                this.register_failing();
              }
            );
    },
  
    //  注册成功后关闭注册对话框
    closeRegister() {
      this.$bus.$emit("closeRegister", this.dialogVisible);
    },

    // 提交编辑信息
    submitUserEditForm() {
          axios
            .post("http://127.0.0.1:3000/messagesManage", {
              _id: this.messagesEdit._id,
              userName: this.messagesEdit.userName,
              message: this.messagesEdit.message,
              messageTime: this.messagesEdit.messageTime,
            })
            .then(
              () => {
                this.EditUserInfo_success();
                this.dialogVisibleTwo = false;
                this.sendUserEditInfo()

              },
              () => {
                this.register_failing();
              }
            );
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClose(tag) {
      this.ruleForm.dynamicTags.splice(
        this.ruleForm.dynamicTags.indexOf(tag),
        1
      );
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.ruleForm.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.cities.length;
    },
    // 注册成功判断
    newUser_success() {
      this.$message({
        showClose: true,
        message: "新增用户数据成功！",
        type: "success",
      });
    },
    // 用户信息编辑成功判断
    EditUserInfo_success() {
      this.$message({
        showClose: true,
        message: "留言更新数据成功！",
        type: "success",
      });
    },
    // 用户信息删除成功判断
    deleteUser_success() {
      this.$message({
        showClose: true,
        message: "留言数据删除成功！",
        type: "success",
      });
    },
    // 注册失败判读
    register_failing() {
      this.$message({
        showClose: true,
        message: "注册账号失败！",
        type: "error",
      });
    },
    sendUserEditInfo(){
      axios.get("http://127.0.0.1:3000/messagesInfo").then((res) => {
      this.messages = res.data;
      console.log(this.messages);
    });
    }
  },
  created() {
    this.sendUserEditInfo()
  },
};
</script>

<style>
</style>